package org.shilong.Utility.Loader;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.shilong.Loader.ImageLoader;
import org.shilong.Model.FriendModel;
import org.shilong.UI.R;
import org.shilong.UI.friend.FriendCenterFrame;
import org.shilong.UI.friend.ShowFriendDetailFrame;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.constants.APIConstants;
import org.shilong.Utility.constants.WeiboConstants;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.weibo.net.Oauth2AccessTokenHeader;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

public class FriendListViewLoader {
	
	private ListView mListView;
	
	private String[] arr;

	private Activity mContext;
	private String wei_id;
	private ProgressDialog dialog;
	public FriendListViewLoader(ListView lv,  Activity con,String wei_id ) {

		this.wei_id = wei_id;
		
		this.mListView = lv;

		this.mContext = con;

		arr = SharedPreferenceUtility
				.getPersonInfoFromSharedPreference(mContext);
		
		
		
	}
	
	
	public void startLoader(){
		
		mHandler.post( new Job( wei_id ) );
	}
	
	
	private Handler mHandler = new Handler();
	
	private class Job implements Runnable{
		
		private String weiboid;
		public Job( String id ){
		
			this.weiboid = id;
			
		
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			dialog = new ProgressDialog(mContext);
			
			dialog.setMessage("获取信息中..");
			
			dialog.show();
			
			//	在loginframe中封装的
			Utility.setAuthorization( new  Oauth2AccessTokenHeader() );
			
			List<FriendModel> list = new ArrayList<FriendModel>();
			
			
			/**
			 * 获取好友的id 列表
			 * 获取好友的具体信息
			 * 
			 */
			WeiboParameters params =  new WeiboParameters();
			
			params.add( "source", WeiboConstants.APIKEY );
			
			params.add("uid", weiboid );	//试试把 
		
			Utility.setAuthorization(new Oauth2AccessTokenHeader());  //	加上之后就不报出现过期的等异常了
		
			
			//获取互粉好友
			try {
				String friendsRlt = Weibo.getInstance().request(mContext, APIConstants.GET_FRIEND,
						params, Utility.HTTPMETHOD_GET,
						Weibo.getInstance().getAccessToken());
				
				list = getFriendList( friendsRlt );
				
				mListView.setAdapter( new MyAdapter(list) );
				
				dialog.dismiss();
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		
	}
	
	/**
	 * 获取好友id的列表
	 * @param rlt
	 * @return
	 */
	private List<FriendModel> getFriendList( String rlt ){

		List<FriendModel> friList = new ArrayList<FriendModel>();
		
		try {
			JSONObject obj = new JSONObject(rlt);
			
			String IDARR = obj.getString("ids");
			

			IDARR  = IDARR.replace("[", "");
			IDARR  = IDARR.replace("]", "");

			String[] arr = IDARR.split(",");

			for (String string : arr) {

				Weibo mWeibo = Weibo.getInstance();

				WeiboParameters params = new WeiboParameters();
				params.add( "uid", string );

				Utility.setAuthorization( new  Oauth2AccessTokenHeader() );
				
				String re = mWeibo.request( mContext , "https://api.weibo.com/2/users/show.json", params, Utility.HTTPMETHOD_GET, mWeibo.getAccessToken());

				JSONObject oo = new JSONObject( re );

				FriendModel model = new FriendModel();

				model.setWeibo_id( oo.getString("id"));
				model.setScreen_name( oo.getString("screen_name"));
				model.setProfile_image_url( oo.getString("profile_image_url"));
				model.setLocation( oo.getString(  "location"  ) );
				model.setDescription( oo.getString( "description" ));
				
				friList.add(model);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
		return friList;

	}
	
	
	private class MyAdapter extends BaseAdapter {

		private List<FriendModel> mList;

		public MyAdapter( List<FriendModel> mList ) {
			
			this.mList = mList;
			
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			
			final FriendModel model = mList.get(arg0);;
			
		/*	model.setWeibo_id( oo.getString("id"));
			model.setScreen_name( oo.getString("screen_name"));
			model.setProfile_image_url( oo.getString("profile_image_url"));
			model.setLocation( oo.getString(  "location"  ) );
			model.setDescription( oo.getString( "description" ));*/
			
			LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
			
			View root = inflater.inflate( R.layout.inner_friend_listview_item_layout, null );
				
			/**
			 * 初始化头像
			 */
			final ImageView iv = (ImageView)root.findViewById( R.id.inner_show_header_img_imageview );

			ImageLoader loader = new ImageLoader();
			
			Drawable draw = loader.loadImage( model.getProfile_image_url() + ".jpg", new ImageLoader.ImageCallBack() {
				
				@Override
				public void setImageDrawable(Drawable imageDrawable) {
					// TODO Auto-generated method stub
					
					iv.setImageDrawable(imageDrawable);
				}
			});
			
			if( draw != null )iv.setImageDrawable(draw);
			
			
			TextView inner_friend_screen_name_textview = (TextView)root.findViewById(R.id.inner_friend_screen_name_textview);
			TextView inner_friend_location_textview = (TextView)root.findViewById(R.id.inner_friend_location_textview);
			TextView inner_friend_desc_textview = (TextView)root.findViewById(R.id.inner_friend_desc_textview );
			
			inner_friend_screen_name_textview.setText( model.getScreen_name() );
			inner_friend_location_textview.setText( model.getLocation() );
			inner_friend_desc_textview.setText( model.getDescription() );
			
			/* 点击item时间 */

			root.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

					Bundle bun = new Bundle();

					bun.putString("image_url", model.getProfile_image_url());
					bun.putString("weibo_id", model.getWeibo_id());

					IntentUtility.goNextFrame(mContext,
							ShowFriendDetailFrame.class, bun);

				}
			});

			
			return root;
		}
	} 

}
